﻿using AdverseEventReportSystem.BLL;
using AdverseEventReportSystem.BLL.Scheduling;
using AdverseEventReportSystem.BLL.SchedulingSystem;
using AdverseEventReportSystem.Core;
using AdverseEventReportSystem.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AdverseEventReportSystem.Web.Areas.Scheduling.Controllers
{
    /// <summary>
    /// 假期类型维护
    /// </summary>
    public class HolidayTypeController : BaseController
    {
        // GET: /Scheduling/HolidayType/
        public ActionResult Index()
        {
            
            return View();
        }

        public ActionResult Search() 
        {
            int pageIndex = WebUtils.GetInt("page", 1);
            int pageSize = WebUtils.GetInt("pagesize", 30);

            OperateResult resultInfo = HolidayTypeManager.GetHolidayTypeList(pageIndex, pageSize);
            string NoData = "{Rows:[],Total:0}";
            if (resultInfo.Result == 1)
            {
                var listData = JsonConvert.DeserializeObject<dynamic>(resultInfo.Body.ToString());
                return JsonDate(listData);
            }
            else
            {
                NoData = "{\"Total\": 0,\"Rows\":[],\"Result\":\"" + resultInfo.Result + "\",\"Body\":\"" + resultInfo.Body + "\"}";
            }
            return Content(NoData);
        }

        public ActionResult Edit()
        {
            HolidayTypeSetting holidayTypeSetting = new HolidayTypeSetting();
            int id = WebUtils.GetInt("id");
            if (id > 0)
            {
                //编辑 取数据赋值
                OperateResult result =  HolidayTypeManager.GetHolidayType(id);
                if (result.Result == 1)
                {
                    holidayTypeSetting = (HolidayTypeSetting)result.Body;
                }
            }
            return View(holidayTypeSetting);
        }

        public ActionResult Save()
        {
            HolidayTypeSetting holidayTypeSetting = new HolidayTypeSetting();
            holidayTypeSetting.CreateTime = DateTime.Now;
            holidayTypeSetting.CreateUserID = LoginManager.GetLoginUser().Id.ToString();
            holidayTypeSetting.DepartmentCode = loginUser.CurrDepCode;
            holidayTypeSetting.Describe = Request["txtDescribe"];
            holidayTypeSetting.Enabled = 1;
            holidayTypeSetting.HolidayTypeName = Request["txtHolidayName"];
            holidayTypeSetting.Id = DataConvert.StrToInt(Request["hdnId"], 0);
            holidayTypeSetting.IsDel = 0;
            holidayTypeSetting.Sort = DataConvert.StrToInt(Request["txtiSort"], 1);
            OperateResult resultInfo = HolidayTypeManager.SaveHolidayType(holidayTypeSetting);
            return JsonDate(resultInfo);
        }

        public ActionResult Enabled()
        {
            int id = WebUtils.GetInt("Id", 0);
            OperateResult resultInfo = new OperateResult();
            if (id > 0)
            {
                resultInfo = HolidayTypeManager.GetHolidayType(id);
                if (resultInfo.Result == 1)
                {
                    HolidayTypeSetting holidayTypeSetting = (HolidayTypeSetting)resultInfo.Body;
                    holidayTypeSetting.Enabled = holidayTypeSetting.Enabled == 0 ? 1 : 0;
                    resultInfo = HolidayTypeManager.SaveHolidayType(holidayTypeSetting);
                }
            }
            return JsonDate(resultInfo);
        }
        public ActionResult Delete()
        {
            int id = WebUtils.GetInt("Id", 0);
            OperateResult resultInfo = new OperateResult();
            if (id > 0)
            {
                resultInfo = HolidayTypeManager.GetHolidayType(id);
                if (resultInfo.Result == 1)
                {
                    HolidayTypeSetting holidayTypeSetting = (HolidayTypeSetting)resultInfo.Body;
                    holidayTypeSetting.IsDel = 1;
                    resultInfo = HolidayTypeManager.SaveHolidayType(holidayTypeSetting);
                }
            }
            return JsonDate(resultInfo);
        }
    }
}